﻿package util {
	import flash.display.Stage;
	import flash.display.DisplayObject;
	import flash.events.KeyboardEvent;
	/**
	 * @author ase7en
	 */
	public class KeyUtil {
		//当前对象实例
		private static var _currentDisplayObj:*;
		private static var _stage:Stage;
		public static function initkeyManager(stage:Stage,currentDisPlayObj:DisplayObject):void
		{
			if(stage)
			{
				_stage = stage;
				_currentDisplayObj = currentDisPlayObj;
				active();
			}else 
			{
				throw "该实例还未添加到舞台，请添加到舞台后在初始化";
			}
		}

		private static function keyDown(event:KeyboardEvent) : void
		{
			_currentDisplayObj.keyAction(event.keyCode);
		}
		//激活键盘事件
		public static function active():void
		{
			if(!_stage.hasEventListener("keyDown")){
				_stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
			}
		}
		//禁止键盘事件
		public static function deactive():void
		{
			_stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDown);
		}
	}
}
